package com.frinder.base.client;

import com.frinder.base.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;

import static com.frinder.base.HeartbeatHandler.HB;

/**
 * @ClassName ClientMessageAdapter
 * @Description 客户端消息适配器，消息处理之前过滤机制，剔除非法消息
 * @Author frinder
 * @Date 2018/11/28 18:39
 * @Version 1.0
 */
@Slf4j
public class ClientMessageAdapter extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ByteBuf) {
            String hb = ByteBufUtils.str((ByteBuf) msg);
            if (HB.equalsIgnoreCase(hb)) {
                // 过滤 心跳检测
                log.info("*** >> HB: {}", hb);
                ReferenceCountUtil.release(msg);
            } else {
                super.channelRead(ctx, msg);
            }
        } else {
            ReferenceCountUtil.release(msg);
        }
    }

}
